<?php

set_time_limit(0);

define('ROOT_PATH', str_replace('Cli/'.basename(__FILE__), '', str_replace('\\', '/', __FILE__)));

require_once(ROOT_PATH."Cls/common.php");
require_once(ROOT_PATH."Cls/db.php");
require_once(ROOT_PATH."Cls/Snoopy.class.php");
require_once(ROOT_PATH."Cls/lib_jd.php");

$id = def_get('id',0);

//初始化
$db = new DB();

if($id > 0){
    //读取需要抓取的列表
    $ranks = $db->get_one("SELECT * FROM jd_rank WHERE item_id !='' AND url != '' AND disabled != 1 AND id=$id");
}else{

    $item_id    = def_post('item_id',0);
    $url        = def_post('url'    ,'');
    $page       = def_post('page'   ,5);

    //即时查询用
    $ranks = array(
        'item_id'   => $item_id,
        'url'       => $url,
        'page'      => $page
    );
}

if(empty($ranks)){
    //没有找到符合的
    echo -1;
    exit;
}else{
    //排名
    $top        = get_item_top($ranks);
    echo $top;
    exit;
}